package com.wangyk.gateway.config;

import org.apache.catalina.filters.CorsFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

/**
 * 跨域的配置
 */
@Configuration
public class CorsConfig
{
    @Bean
    public CorsFilter corsFilter()
    {
        final UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config=new CorsConfiguration();
        //是否允许cookie跨域
        config.setAllowCredentials(true);
        config.setAllowedOrigins(Arrays.asList("*"));//域名:http://www.a.com,http://www.b.com
        config.setAllowedHeaders(Arrays.asList("*"));//设置头
        config.setAllowedMethods(Arrays.asList("*"));//允许所有的方法
        config.setMaxAge(3600l);//设置缓存时间
        return new CorsFilter();
    }
}
